package com.yike.base.controller;

import com.yike.base.param.FilePageParam;
import com.yike.base.service.FileService;
import com.yike.base.service.QrcodeService;
import com.yike.base.vo.FileVo;
import com.yike.common.api.ApiResult;
import com.yike.common.log.Module;
import com.yike.common.pagination.Paging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 二维码控制器
 *
 * @author Sakura
 * @since 2024-09-30
 */
@Slf4j
@RestController
@RequestMapping("/qrcode")
@Module("base")
@Api(value = "二维码api", tags = {"二维码管理"})
public class QrcodeController {

    @Autowired
    private QrcodeService qrcodeService;

    @PostMapping("/generate")
    @ApiOperation(value = "生成二维码", response = ApiResult.class)
    public ApiResult<String> generate(@RequestParam(value = "text") String text,
                                      @RequestParam(value = "remark", required = false) String remark,
                                      @RequestParam(value = "width", required = false, defaultValue = "300") Integer width,
                                      @RequestParam(value = "height", required = false, defaultValue = "300") Integer height) throws Exception {
        String path = qrcodeService.generate(text, remark, width, height);
        return ApiResult.ok(path);
    }

    @PostMapping("/generateWithLogo")
    @ApiOperation(value = "生成带logo二维码", response = ApiResult.class)
    public ApiResult<String> generateWithLogo(@RequestPart("file") MultipartFile file,
                                              @RequestParam(value = "text") String text,
                                              @RequestParam(value = "remark", required = false) String remark,
                                              @RequestParam(value = "width", required = false, defaultValue = "300") Integer width,
                                              @RequestParam(value = "height", required = false, defaultValue = "300") Integer height) throws Exception {
        String path = qrcodeService.generateWithLogo(file, text, remark, width, height);
        return ApiResult.ok(path);
    }

    /**
     * 下载文件
     */
    @GetMapping("/{code}")
    @ApiOperation(value = "下载")
    public void download(HttpServletResponse response, @PathVariable("code") String code) throws Exception {
        qrcodeService.download(response, code);
    }

}

